<?
session_start();

@include_once('config.php');

@include_once('functions_db.php');

function drizzo_install_check() {
	global $drizzo_options;

	if(!file_exists('config.php')) {
		$is_installed = 0;
	} else {

		drizzo_db_connect();

		$drizzo_install_check = @mysql_query("SELECT `ID` FROM `$drizzo_options` WHERE `name`='install_timestamp'");
		if(!mysql_errno()) {
			$is_installed = @mysql_num_rows($drizzo_install_check);
		} else {
			$is_installed = 0;
		}
	}

	return $is_installed;
}

function install_drizzo() {
	
	global $drizzo_comments, $drizzo_events, $drizzo_gallery, $drizzo_links, $drizzo_options, $drizzo_posts, $drizzo_users, $drizzo_trashheap, $drizzo_warehouse;
	
	$sql_comments = "CREATE TABLE IF NOT EXISTS `$drizzo_comments` (
	  `ID` int(11) NOT NULL auto_increment,
	  `post_id` int(11) NOT NULL default '0',
	  `parent_id` int(11) NOT NULL default '0',
	  `date` varchar(14) NOT NULL,
	  `content` longtext NOT NULL,
	  `title` varchar(255) default NULL,
	  `tags` text,
	  `author_id` int(11) default NULL,
	  `author_name` varchar(32) default NULL,
	  `author_email` varchar(100) default NULL,
	  `author_url` varchar(255) default NULL,
	  `author_ip` varchar(24) NOT NULL default '',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `status` enum('moderated','approved','spam') NOT NULL default 'moderated',
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `content` (`content`)
	) TYPE=MyISAM;";

	$sql_events = "CREATE TABLE IF NOT EXISTS `$drizzo_events` (
	  `ID` int(11) NOT NULL auto_increment,
	  `start` varchar(14) NOT NULL,
	  `end` varchar(14) NOT NULL,
	  `timezone_offset` VARCHAR(6) NULL,
	  `what` longtext NOT NULL,
	  `where` longtext,
	  `address` longtext,
	  `who` longtext,
	  `notes` longtext,
	  `tags` longtext,
	  `added_by` int(11) NOT NULL default '0',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `status` ENUM( 'publish', 'draft' ) NOT NULL DEFAULT 'draft',
	  `permalink_name` varchar(255) default NULL,
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_gallery = "CREATE TABLE IF NOT EXISTS `$drizzo_gallery` (
	  `ID` int(11) NOT NULL auto_increment,
	  `title` varchar(64) NOT NULL default '',
	  `description` text NOT NULL,
	  `tags` longtext,
	  `file` longblob,
	  `file_size` varchar(16) default NULL,
	  `file_type` varchar(16) default NULL,
	  `thumb` longblob,
	  `thumb_size` varchar(16) default NULL,
	  `thumb_type` varchar(16) default NULL,
	  `auto_thumb` tinyint(2) NOT NULL default '0',
	  `date_added` varchar(14) NOT NULL,
	  `owner` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_links = "CREATE TABLE IF NOT EXISTS `$drizzo_links` (
	  `ID` int(11) NOT NULL auto_increment,
	  `name` varchar(64) NOT NULL default '',
	  `url` varchar(100) NOT NULL default '',
	  `tags` longtext NOT NULL,
	  `owner` int(11) NOT NULL default '0',
	  `added` varchar(14) NOT NULL,
	  `image` TEXT NULL,
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";
	$sql_options = "CREATE TABLE IF NOT EXISTS `$drizzo_options` (
	  `ID` int(11) NOT NULL auto_increment,
	  `name` varchar(100) NOT NULL default '',
	  `value` varchar(100) NOT NULL default '',
	  `owner` int(11) default NULL,
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";

	$sql_posts = "CREATE TABLE IF NOT EXISTS `$drizzo_posts` (
	  `ID` int(11) NOT NULL auto_increment,
	  `author` int(11) NOT NULL default '0',
	  `type` enum('blog','review','page','podcast','aside') NOT NULL default 'blog',
	  `status` enum('publish','draft','private') NOT NULL default 'publish',
	  `page_parent` int(11) NOT NULL default '0',
	  `page_order` int(11) NOT NULL default '0',
	  `title` longtext NOT NULL,
	  `content` longtext NOT NULL,
	  `tags` longtext,
	  `comment_status` enum('open','closed') NOT NULL default 'open',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `posted_date` varchar(14) NOT NULL,
	  `import_id` int(11) default NULL,
	  `permalink_name` varchar(255) default NULL,
	  `menu_name` varchar(100) default NULL,
	  `lat` varchar(12) default NULL,
	  `lon` varchar(12) default NULL,
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `title` (`title`,`content`)
	) TYPE=MyISAM;";
	
	$sql_trashheap = "CREATE TABLE IF NOT EXISTS `$drizzo_trashheap` (
	  `ID` int(11) NOT NULL auto_increment,
	  `author` int(11) NOT NULL default '0',
	  `type` enum('blog','review','page','podcast','aside') NOT NULL default 'blog',
	  `status` enum('publish','draft','private') NOT NULL default 'publish',
	  `page_parent` int(11) default NULL,
	  `page_order` int(11) default NULL,
	  `title` longtext NOT NULL,
	  `content` longtext NOT NULL,
	  `tags` longtext,
	  `comment_status` enum('open','closed') NOT NULL default 'open',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `posted_date` varchar(14) NOT NULL,
	  `permalink_name` varchar(255) default NULL,
	  `menu_name` varchar(100) default NULL,
	  `lat` varchar(12) default NULL,
	  `lon` varchar(12) default NULL,
	  `deleted_by` int(11) NOT NULL default '0',
	  `deleted_date` varchar(14) NOT NULL,
	  `old_id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `title` (`title`,`content`)
	) TYPE=MyISAM;";


	$sql_users = "CREATE TABLE IF NOT EXISTS `$drizzo_users` (
	  `ID` int(11) NOT NULL auto_increment,
	  `first_name` varchar(32) NOT NULL default '',
	  `last_name` varchar(32) NOT NULL default '',
	  `nice_name` varchar(32) default NULL,
	  `gender` varchar(6) default NULL,
	  `url` text,
	  `aim` varchar(64) default NULL,
	  `msn` varchar(64) default NULL,
	  `google_talk` varchar(64) default NULL,
	  `jabber` varchar(64) default NULL,
	  `yahoo` varchar(64) default NULL,
	  `email` varchar(100) default NULL,
	  `user_level` tinyint(2) NOT NULL default '0',
	  `date_added` varchar(14) NOT NULL,
	  `date_edited` varchar(14) NOT NULL,
	  `edited_by` tinyint(4) NOT NULL default '0',
	  `avatar` longblob,
	  `myspace` varchar(32) default NULL,
	  `facebook` varchar(32) default NULL,
	  `digg` varchar(32) default NULL,
	  `lastfm` varchar(32) default NULL,
	  `delicious` varchar(32) default NULL,
	  `about` longtext,
	  `username` varchar(64) NOT NULL default '',
	  `username_md5` varchar(100) default NULL,
	  `password` varchar(100) NOT NULL default '',
	  PRIMARY KEY  (`ID`)
	) TYPE=MyISAM;";
	
	$sql_warehouse = "CREATE TABLE IF NOT EXISTS `$drizzo_warehouse` (
	  `ID` int(11) NOT NULL auto_increment,
	  `author` int(11) NOT NULL default '0',
	  `type` enum('blog','review','page','podcast','aside') NOT NULL default 'blog',
	  `status` enum('publish','draft','private') NOT NULL default 'publish',
	  `page_parent` int(11) default NULL,
	  `page_order` int(11) default NULL,
	  `title` longtext NOT NULL,
	  `content` longtext NOT NULL,
	  `tags` longtext,
	  `comment_status` enum('open','closed') NOT NULL default 'open',
	  `edited_by` int(11) NOT NULL default '0',
	  `edited_date` varchar(14) NOT NULL,
	  `posted_date` varchar(14) NOT NULL,
	  `permalink_name` varchar(255) default NULL,
	  `menu_name` varchar(100) default NULL,
	  `lat` varchar(12) default NULL,
	  `lon` varchar(12) default NULL,
	  `old_id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`ID`),
	  FULLTEXT KEY `title` (`title`,`content`)
	) TYPE=MyISAM;";
	
	if(gmdate('U') - date('U') > 0) {
		$offset = "-" . gmdate('U') - date('U');
	} elseif(date('U') - gmdate('U')) {
		$offset = "+" . date('U') - gmdate('U');
	} else {
		$offset = "0";
	}
	$sql_default_options = "INSERT INTO `$drizzo_options` VALUES ('', 'install_timestamp', '" . time() . "', NULL),
	('', 'use_permalinks', '1', NULL),
	('', 'timezone_offset', '$offset', NULL),
	('', 'dst', '1', NULL),
	('', 'warehouse_limit', '10', NULL),
	('', 'warehouse_use', '1', NULL),
	('', 'site_name', '" . drizzo_db_wrap($_POST['site_name']) . "', NULL),
	('', 'site_description', '" . drizzo_db_wrap($_POST['site_tagline']) . "', NULL),
	('', 'theme', 'minimal', NULL),
	('', 'time_format', 'F d, Y \\\\a\\\\t g:i A', NULL),
	('', 'posts_per_page', '10', NULL),
	('', 'module_blog', 1, NULL),
	('', 'module_pages', 1, NULL),
	('', 'module_comments', 1, NULL),
	('', 'module_podcasts', 0, NULL),
	('', 'module_reviews', 0, NULL),
	('', 'module_events', 0, NULL),
	('', 'module_gallery', 0, NULL),
	('', 'module_links', 0, NULL),
	('', 'module_users', 1, NULL),
	('', 'module_plugins', 1, NULL),
	('', 'gallery_basename', 'gallery', NULL),
	('', 'events_basename', 'events', NULL);";

	drizzo_db_connect();
	
	@mysql_query($sql_comments) or die("<p>Comments Table creation failed.<br />" . mysql_error() . "</p>\n");
	$status .= "<p>Comments Table creation successful.</p>\n";

	@mysql_query($sql_events) or die("<p>Events Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Events Table creation successful.</p>\n";

	@mysql_query($sql_gallery) or die("<p>Gallery Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Gallery Table creation successful.</p>\n";

	@mysql_query($sql_links) or die("<p>Links Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Links Table creation successful.</p>\n";

	@mysql_query($sql_options) or die("<p>Options Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Options Table creation successful.</p>\n";

	@mysql_query($sql_default_options) or die("<p>Default Options could not be entered.<br />" . mysql_error() . "</p>\n");
	$status .= "<p>Default Options set up.</p>\n";

	@mysql_query($sql_posts) or die("<p>Posts Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Posts Table creation successful.</p>\n";

	@mysql_query($sql_trashheap) or die("<p>TrashHeap Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>TrashHeap Table creation successful.</p>\n";

	@mysql_query($sql_users) or die("<p>Users Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Users Table creation successful.</p>\n";

	@mysql_query($sql_warehouse) or die("<p>Warehouse Table creation failed." . mysql_error() . "</p>\n");
	$status .= "<p>Warehouse Table creation successful.</p>\n";
	return $status;
}

$is_installed = drizzo_install_check();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		
		<title>Drizzo</title>
		
		<link rel="stylesheet" type="text/css" href="../styles/drizzo/drizzo.css" />

	</head>
	
	<body>
		
		<div id="header">

			<h1>Drizzo Installer</h1>
			
			<ul>
				<li class="section_header">Installation Progress</li>
<?
	if(!$_POST) {
		echo "<li style=\"color: #aeaeae;\"><a>1. Site Information</a></li>\n";
	} else {
		echo "<li><a>1. Site Information</a></li>\n";
	}
	if($_POST['install'] == "true" && $_POST['import_wordpress'] == 'on') {
		echo "<li style=\"color: #aeaeae;\"><a>2. Import Previous Blog (Optional)</a></li>\n";
	} else {
		echo "<li><a>2. Import Previous Blog (Optional)</a></li>\n";
	}
	if(($_POST['install'] == 'true' && $_POST['import_wordpress'] != 'on') || $_POST['make_admin'] == 'true') {
		echo "<li style=\"color: #aeaeae;\"><a>3. Create Administrator</a></li>\n";
	} else {
		echo "<li><a>3. Create Administrator</a></li>\n";
	}
?>
				<br class="clear" />
			</ul>
		</div>
		
		<div id="sidebar">
			<ul>
				<li>&nbsp;</li>
				<br class="clear" />
			</ul>
		</div>
		
		<div id="wrapper">

		<div id="main">
<?	
	if(($is_installed > 0) && !$_POST) { // Drizzo is already installed
?>
			<h2>Drizzo Installer</h2>
			<br class="clear" />
			<p>It appears that Drizzo is already installed.<br /><a href="/drizzo" title="Admin Area">Click here to go to the Admin Area.</a></p>
<?
	} elseif(!file_exists('config.php')) { // The config file is missing
?>
			<p>Drizzo cannot find /drizzo/engine/config.php. Please edit /drizzo/engine/config_sample.php and rename it to config.php then refresh this page.</p>
<?		
	} elseif(file_exists('config.php') && $_POST['install'] != "true" && $is_installed == 0) { // Step 1
?>	
			<p>Your configuration file has been found. Please check the box if you would like to import a WordPress blog.  Then, please click "Start the Install" to go to the next step.</p>
				<form id="installer" name="installer" method="post" action="">
					<label for="site_name">Site Name <a href="javascript:void(0)">?<span class="explain">What you want to appear as the title of your site.</span></a></label><input type="text" name="site_name"></input><br />
					<label for="site_tagline">Site Tagline <a href="javascript:void(0)">?<span class="explain">What you want to appear as description/tagline/motto of your site.  You can leave this blank.</span></a></label><input type="text" name="site_tagline"></input><br />
					<label for="import_wordpress">Import WordPress <a href="javascript:void(0)">?<span class="explain">If you currently have a WordPress blog on this server, check this option to be able to import its contents in step 2.</span></a></label><input type="checkbox" class="checkbox" name="import_wordpress"></input><br />
					<input type="hidden" name="install" value="true"></input>
					<input type="submit" name="submit" class="submit" value="Start the Install" onclick="document.getElementById('submit').value = 'Installing...'"></input>
				</form>
			</p>
<?
	} elseif(file_exists('config.php') && $_POST['install'] == 'true') { // Step 2
?>
	<h2>Drizzo Installation Progess</h2>
	<br class="clear" />
<?
		echo install_drizzo();
		
		if($_POST['import_wordpress'] == 'on') { // Step 3 A (Optional)
?>	
	<p>&nbsp;</p>

	<h2>Import WordPress Data</h2>
	<br class="clear" />
	<p>
		<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="installer" id="installer" class="wp">
			<label for="wp_db">WordPress Database Name <a href="javascript:void(0)">?<span class="explain">Enter the name of the database WordPress used.</span></a></label><input type="text" name="wp_db"></input><br />
			<label for="wp_db_host">WordPress Database Host <a href="javascript:void(0)">?<span class="explain">Enter the name of the WordPress database host. Use 'localhost' if you are unsure.</span></a></label><input type="text" name="wp_db_host" value="localhost"></input><br />
			<label for="wp_db_user">WordPress Database Username</label><input type="text" name="wp_db_user"></input><br />
			<label for="wp_db_pass">WordPress Database Password</label><input type="password" name="wp_db_pass"></input><br />
			<label for="wp_db_prefix">WordPress Database Prefix <a href="javascript:void(0)">?<span class="explain">If WordPress used a table prefix, enter it here. (WordPress uses wp_ by default.)</span></a></label><input type="text" name="wp_db_prefix" value="wp_"></input><br />
			<input type="hidden" name="wp_import" value="true"></input>
			<input type="submit" name="submit" class="submit"  onclick="document.getElementById('submit').value = 'Importing...';document.getElementById('submit').type = 'button';document.getElementById('submit').onclick = '';" value="Import WordPress Data"></input>
		</form>
	</p>
	<p>Please only click "Import WordPress Data" once. Importing <em>will</em> take a few minutes.</p>
<?
		} else { // Step 4 A
?>
	<p>&nbsp;</p>

	<h2>Create Administrator Account</h2>
	<br class="clear" />
	<p>
		<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="make_admin" id="installer">
			<label for="username">Username</label><input type="text" name="username"></input><br />
			<label for="password">Password</label><input type="password" name="password"></input><br />
			<label for="password2">Verify Password</label><input type="password" name="password2"></input><br />
			<label for="username">Email</label><input type="text" name="email"></input><br />
			<input type="hidden" name="make_administrator" value="true"></input>
			<input type="submit" name="submit" class="submit"  onclick="document.getElementById('submit').value = 'Creating...'" value="Create Administrator"></input>
		</form>
	</p>
<?
		}
	}

	if($_POST['wp_import'] == "true") { // Step 3 B (Optional)
		include('import_wp.php');
	}
	
	if(($_POST['make_administrator'] == "true") && ($_POST['password'] == $_POST['password2'])) {  // Step 4 B (if passwords match)
		
		$username = $_POST['username'];
		$password = md5($_POST['password']);
		$email = $_POST['email'];
		$username_md5 = md5($username);
		drizzo_db_connect();
		$admin_create = @mysql_query("INSERT INTO `$drizzo_users` (`username`, `password`, `email`, `username_md5`, `user_level`) VALUES ('$username', '$password', '$email', '$username_md5', '5')");
		if(mysql_errno()) {
			echo "<p>Database error. User was not created.</p>" . mysql_error();
		} else {
?>
	<h2>Installation Complete</h2>
	<br class="clear" />
	<p>That's it! <a href="/drizzo">Click here to go login and get started.</a></p>
<?
		}
	}
	
	if(($_POST['make_administrator'] == "true") && ($_POST['password'] != $_POST['password2'])) { // Step 4 B (if passwords do not match)
?>
	<h2>Create Administrator Account</h2>
	<br class="clear" />
	<p>Passwords did not match. Please try again.</p>

	<p>
		<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>" name="make_admin" id="installer">
			<label for="username">Username</label><input type="text" name="username" value="<? echo $_POST['username']; ?>"></input><br />
			<label for="password">Password</label><input type="password" name="password"></input><br />
			<label for="password2">Verify Password</label><input type="password" name="password2"></input><br />
			<label for="username">Email</label><input type="text" name="email" value="<? echo $_POST['email']; ?>"></input><br />
			<input type="hidden" name="make_administrator" value="true"></input>
			<input type="submit" name="submit" class="submit"  onclick="document.getElementById('submit').value = 'Creating...'" value="Create Administrator"></input>
		</form>
	</p>
<?
	}
?>
	<br class="clear" />
	</div>
	</div>
<?
	$admin_location = str_replace('engine','',dirname(__FILE__));
	include($admin_location . "footer.php");
?>
